package com.zbbt.sportslottery.ui.adapter;

import java.util.List;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.bean.ColumnBean;

public class ChannelAdapter extends BaseAdapter {
	/** 是否显示底部的ITEM */
	private boolean isItemShow = false;

	private Context context;

	private List<ColumnBean> userChannelList;
	/** 控制的postion */
	private int holdPosition;
	/** 是否改变 */
	private boolean isChanged = false;
	/** 列表数据是否改变 */
	public boolean isListChanged = false;
	/** 是否可见 */
	boolean isVisible = true;
	/** TextView 频道内容 */
	private TextView item_text;
	/** 要删除的position */
	public int remove_position = -1;

	public ChannelAdapter(Context context, List<ColumnBean> userChannelList) {
		this.context = context;
		this.userChannelList = userChannelList;
	}

	@Override
	public int getCount() {
		return userChannelList == null ? 0 : userChannelList.size();
	}

	@Override
	public ColumnBean getItem(int position) {
		if (userChannelList != null && userChannelList.size() != 0) {
			return userChannelList.get(position);
		}
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("NewApi")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View view = View.inflate(context, R.layout.item_channel, null);
		item_text = (TextView) view.findViewById(R.id.text_item);
		ColumnBean channelItem = getItem(position);
		item_text.setText(channelItem.getName());
		if ((position == 0 )) {
			item_text.setEnabled(false);// 设置没有焦点
			item_text.setBackgroundResource(R.drawable.user_first);
		}
		// 此时的状态是手势在触发的过程中/item 所处在的位置
		if (isChanged && (position == holdPosition) && !isItemShow) {
			item_text.setText("");
			item_text.setSelected(true);
			item_text.setEnabled(true);
			isChanged = false;
		}
		if (!isVisible && (position == -1 + userChannelList.size())) {
			item_text.setText("");
			item_text.setSelected(true);
			item_text.setEnabled(true);
		}
		if (remove_position == position) {
			item_text.setText("");
		}
		return view;
	}

	/** 添加频道列表 */
	public void addItem(ColumnBean channel) {
		userChannelList.add(channel);
		isListChanged = true;
		notifyDataSetChanged();
	}
	/** 获取频道列表 */
	public List<ColumnBean> getChannnelLst() {
		return userChannelList;
	}
	/** 设置删除的position */
	public void setRemove(int position) {
		remove_position = position;
		notifyDataSetChanged();
	}
	/** 显示放下的ITEM */
	public void setShowDropItem(boolean show) {
		isItemShow = show;
	}

	/**
	 * 
	 * @MethodName:exchange
	 * @Description: 拖动变更频道排序
	 * @author：GaomingShuo
	 * @date：2015-2-4 下午9:32:11
	 * @param dragPostion
	 * @param dropPostion
	 *            void
	 */
	public void exchange(int dragPostion, int dropPostion) {
		holdPosition = dropPostion;
		ColumnBean dragItem = getItem(dragPostion);
		// Log.d(TAG, "startPostion=" + dragPostion + ";endPosition=" +
		// dropPostion);
		if (dragPostion < dropPostion) {
			userChannelList.add(dropPostion + 1, dragItem);
			userChannelList.remove(dragPostion);
		} else {
			userChannelList.add(dropPostion, dragItem);
			userChannelList.remove(dragPostion + 1);
		}
		isChanged = true;
		isListChanged = true;
		notifyDataSetChanged();
	}
	/** 删除频道列表 */
	public void remove() {
		userChannelList.remove(remove_position);
		remove_position = -1;
		isListChanged = true;
		notifyDataSetChanged();
	}
	
	/** 设置频道列表 */
	public void setListDate(List<ColumnBean> list) {
		userChannelList = list;
	}
	
	/** 获取是否可见 */
	public boolean isVisible() {
		return isVisible;
	}
	
	/** 排序是否发生改变 */
	public boolean isListChanged() {
		return isListChanged;
	}
	
	/** 设置是否可见 */
	public void setVisible(boolean visible) {
		isVisible = visible;
	}
	
}
